clear all

// SET MACROS
global Input  "Y:/limited/Michigan_CTE/funding_change/data_final"
global Output "Y:/limited/Michigan_CTE/funding_change/output/figures"

qui include "Y:/limited/Michigan_CTE/funding_change/code/analyze/figures/00_colors.do"

local demographics "female black hisp another_race sped lep econdis"
local academics "test_avg_g8 test_avg_sq_g8 test_avg_g8_imp attendance_g8 attendance_g8_imp"

// LOAD DATA
use $Input/dcode_funding.dta, clear

keep if inrange(year,2012, 2019)


foreach v in cte_funds_state60 cte_funds_cepd40 cte_funds_total {
	gen all_`v'_n = dcode_`v' + isd_`v'_n
}

foreach v of varlist all_cte_funds_* {
	bys year: gegen all_`v' = total(`v')

	bys dcode: gegen pre_`v'  = mean(cond(year<2015,`v',.))
	bys dcode: gegen post_`v' = mean(cond(year>=2017,`v',.))

	gen pct_`v' = (100 * `v') / all_`v'
	
	bys dcode: gegen pre_pct_`v'  = mean(cond(year<2015,pct_`v',.))
	bys dcode: gegen post_pct_`v' = mean(cond(year>=2017,pct_`v',.))

	gen ps_`v' = `v' / hs_students
	
	bys dcode: gegen pre_ps_`v'  = mean(cond(year<2015,`v'/hs_students,.))
	bys dcode: gegen post_ps_`v' = mean(cond(year>=2017,`v'/hs_students,.))
}

foreach v of varlist pre* post* {
	replace `v' = 0 if missing(`v')
}

forval i = 2012/2019 {
	gen c`i' = year == `i' 
}
replace c2014 = 0

// RUN REGRESSIONS
gen cohort = .
gen fund1_over_time     = .
gen fund1_over_time_l95 = .
gen fund1_over_time_u95 = .
gen fund2_over_time     = .
gen fund2_over_time_l95 = .
gen fund2_over_time_u95 = .
gen fund3_over_time     = .
gen fund3_over_time_l95 = .
gen fund3_over_time_u95 = .
gen fund4_over_time     = .
gen fund4_over_time_l95 = .
gen fund4_over_time_u95 = .

forvalues n = 1/4 {
	eststo: reghdfe ps_all_cte_funds_total c2012-c2019 if district_urbanicity == `n' [aw=hs_students], a(dcode) cluster(dcode)
	local i 1
	forvalues x = 2012/2019 {
	  replace fund`n'_over_time =  _b[c`x'] in `i'
	  replace fund`n'_over_time_l95 =  _b[c`x'] - (invttail(e(df_r),0.025) *  _se[c`x']) in `i'
	  replace fund`n'_over_time_u95 =  _b[c`x'] + (invttail(e(df_r),0.025) *  _se[c`x']) in `i'
	  replace cohort = `x' if cohort==. in `i'
	  local ++i
	}
}

keep cohort fund1* fund2* fund3* fund4* 
drop if missing(cohort)

// DRAW GRAPH
tw (rarea fund4_over_time_l95 fund4_over_time_u95 cohort, color("$c4%30") lwidth(none)) ///
   (rarea fund3_over_time_l95 fund3_over_time_u95 cohort, color("$c3%30") lwidth(none)) ///
   (rarea fund2_over_time_l95 fund2_over_time_u95 cohort, color("$c2%30") lwidth(none)) ///
   (rarea fund1_over_time_l95 fund1_over_time_u95 cohort, color("$c1%30") lwidth(none)) ///
   (connected fund4_over_time fund3_over_time fund2_over_time fund1_over_time cohort, sort ///
   mc("$c4" "$c3" "$c2" "$c1") ///
   m(S D T O) ///
   lc("$c4" "$c3" "$c2" "$c1") ///
   lp(dash_dot shortdash dash solid)), ///
   scale(1.1) ///
   xline(2014.5 2015.5, lc(gs12) lp(-)) ///
   yline(0, lc(gs11)) ///
   text(53.1 2014.5 "Funding" "increased," "new formula" "announced", placement(0) color(black) box bcolor(white) margin(b=1)) ///
   text(57. 2015.5 "New" "formula" "applied", placement(0) color(black) box bcolor(white) margin(t=2 b=1)) ///
   xlabel(2012/2019) ///
   xtitle(" " "Cohort Graduation Year", margin(t=2)) ///
   ylabel(-20 "-$20" 0 "$0" 20 "$20" 40 "$40" 60 "$60" , angle(0) glc(gs13) glw(vthin) glp(shortdash) gmax gmin) ///
   ytitle("Change in Per-Student CTE Funding" " ") ///
   legend(order(8 7 6 5) label(8 "Urban") label(7 "Suburban") label(6 "Town") label(5 "Rural") rows(1) symxsize(7) region(lstyle(none))) ///
   graphregion(color(white)) bgcolor(white) ///
   xsize(7) ysize(3.25)  ///
	 scheme(s2color)

// EXPORT GRAPH

graph save $Output/06a_reg_dollar_urbanicity.gph, replace
*graph export $Output/reg_dollar_urbanicity.png, replace
